import request from "./request";
const handleApiResponse =
	(apiFunc) =>
		async (...args) => {
			try {
				const response = await apiFunc(...args);
				console.log('response',response);
				if (response.data.code === 200) {
					return response.data.data; // 返回数据部分
				} else {
					throw new Error(response.data.msg || "API 请求失败");
				}
			} catch (error) {
				throw new Error(error.response ? error.response.data.msg : error.msg);
			}
		};
//  业务API

// 获取首页顶部的banner
const getBanner = handleApiResponse(async () => {
	let options = {
		url: "g-getBanner",
		method: "get",
	};
	return request.httpRequest(options, {});
});
// 获取首页推荐列表
const getRecommendList = handleApiResponse(async () => {
	let options = {
		url: "g-getRecommend",
		method: "get",
	};
	return request.httpRequest(options, {});
});
//商品详情
const getGoodsDetail = handleApiResponse(async (id) => {
	let params = {
		id: id,
	};
	let options = {
		url: "g-getGoodsDetail",
		method: "get",
	};
	return request.httpRequest(options, params);
});
// 登录检查
const loginWithCode = handleApiResponse(async (code) => {
	const params = {
		code,
	};
	const options = {
		url: "a-wxlogin_dl",
		method: "post",
	};
	return request.httpRequest(options, params);
});
// 上传头像
const uploadAvatar = handleApiResponse(async (filePath) => {	
	const res=await uni.uploadFile({
		url: request.baseUrl + "p-icon",
		filePath: filePath,
		name: "image",
	});
	let data={
		'data':JSON.parse(res.data),
	};
	return data;
});
// 登录
const finalizeLogin = handleApiResponse(async (data) => {
	const options = {
		url: "a-wxlogin",
		method: "post",
	};
	return request.httpRequest(options, data);
});
//积分流水(source = 2)
const getIntegralDetail = handleApiResponse(async (uid, source = 2) => {
	let params = {
		uid: uid,
		source: source,
	};
	let options = {
		url: "u-AccountLogList",
		method: "get",
	};
	return request.httpRequest(options, params);
});
//用户信息
const getUserIntegral = handleApiResponse(async (uid) => {
	let params = {
		uid: uid,
	};
	let options = {
		url: "u-role_config",
		method: "post",
	};
	return request.httpRequest(options, params);
});
//商品列表
const getGoodsList = handleApiResponse(async (data) => {
	let options = {
		url: "g-goodsList",
		method: "get",
	};
	return request.httpRequest(options, data);
});
//用户订单列表
const getOrderList = handleApiResponse(async (data) => {
	let options = {
		url: "o-lists",
		method: "get",
	};
	return request.httpRequest(options, data);
});
//订单详情
const getOrderDetail = handleApiResponse(async (uid, id) => {
	let params = {
		uid: uid,
		id: id,
	};
	let options = {
		url: "o-detail",
		method: "get",
	};
	return request.httpRequest(options, params);
});
//取消订单
const cancelOrder = handleApiResponse(
	async (uid, order_id, description = "") => {
		let params = {
			uid,
			order_id,
			...(description && { description }),
		};

		let options = {
			url: "o-cancel",
			method: "post",
		};

		return request.httpRequest(options, params);
	}
);
// 兑换商品
const exchangeGoods = handleApiResponse(async (data) => {
	let options = {
		url: "o-buy",
		method: "post",
	};
	return request.httpRequest(options, data);
});
// 核销订单
const verifyOrder = handleApiResponse(async (uid, pickup_code) => {
	let params = {
		uid,
		pickup_code,
	};
	let options = {
		url: "o-verify",
		method: "post",
	};
	return request.httpRequest(options, params);
});
//确认核销订单
const confirmVerifyOrder = handleApiResponse(
	async (uid, selffetch_shop_id, id) => {
		let params = {
			uid,
			selffetch_shop_id,
			id,
		};
		let options = {
			url: "o-order_write_off_confirm",
			method: "post",
		};
		return request.httpRequest(options, params);
	}
);
//公共配置
const getConfig = handleApiResponse(async () => {
	let options = {
		url: "p-public_config",
		method: "get",
	};
	return request.httpRequest(options, {});
});
//图片上传
const uploadImage = handleApiResponse(async (data) => {
	let options = {
		url: "p-icon",
		method: "post",
	};
	return request.httpRequest(options, data);
});


//问题反馈
const feedback = handleApiResponse(async (uid, description) => {
	let params = {
		uid,
		description,
	};
	let options = {
		url: "u-feedback",
		method: "post",
	};
	return request.httpRequest(options, params);
});
// 收货地址列表
const getAddressList = handleApiResponse(async (uid) => {
	let params = {
		uid,
	};
	let options = {
		url: "ua-lists",
		method: "get",
	};
	return request.httpRequest(options, params);
});
// 新增一条收货地址
const addAddress = handleApiResponse(async (data) => {
	let options = {
		url: "ua-add",
		method: "post",
	};
	return request.httpRequest(options, data);
});
// 修改收货地址
const updateAddress = handleApiResponse(async (data) => {
	let options = {
		url: "ua-update",
		method: "post",
	};
	return request.httpRequest(options, data);
})
//设为默认地址
const setDefaultAddress = handleApiResponse(async (uid, id) => {
	let params = {
		uid,
		id,
	};
	let options = {
		url: "ua-setDefault",
		method: "post",
	};
	return request.httpRequest(options, params);
});
//删除收货地址
const deleteAddress = handleApiResponse(async (uid, id) => {
	let params = {
		uid,
		id,
	};
	let options = {
		url: "ua-del",
		method: "post",
	};
	return request.httpRequest(options, params);
});
//获取一条地址的详细信息
const getAddressDetail = handleApiResponse(async (uid, id) => {
	let params = {
		uid,
		id,
	};
	let options = {
		url: "ua-detail",
		method: "get",
	};
	return request.httpRequest(options, params);
});
//关键字搜索商品
const searchGoods = handleApiResponse(async (keyword) => {
	let params = {
		keyword
	};
	let options = {
		url: 'g-search',
		method: 'get'
	};
	return request.httpRequest(options, params);
})
export default {
	getBanner,
	getRecommendList,
	getGoodsDetail,
	loginWithCode,
	uploadAvatar,
	finalizeLogin,
	getIntegralDetail,
	getUserIntegral,
	getGoodsList,
	getOrderList,
	getOrderDetail,
	cancelOrder,
	exchangeGoods,
	verifyOrder,
	confirmVerifyOrder,
	getConfig,
	uploadImage,
	feedback,
	getAddressList,
	addAddress,
	updateAddress,
	setDefaultAddress,
	deleteAddress,
	getAddressDetail,
	searchGoods
};
